package kr.hkit.summary_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.dao.DepartmentDAO;
import kr.hkit.summary_java.erp.dto.Department;

public class PanelDepartmentUI extends JPanel implements ActionListener, FocusListener {
	private static final long serialVersionUID = 1L;
	
	private InputPanel pDeptno;
	private InputPanel pDeptName;
	private InputPanel pFloor;
	
	private JPanel panel_Bottom;
	private JPanel panel_Center;
	
	private JButton btnAdd;
	private String btnTxt;
	private DepartmentDAO dao;

	public PanelDepartmentUI(String btnTxt, DepartmentDAO dao) {
		this.btnTxt=btnTxt;
		this.dao = dao;
		initialize();
	}
	
	private void initialize() {
		setPreferredSize(new Dimension(240, 160));
		setLayout(new BorderLayout(0, 0));
		
		panel_Center = new JPanel();
		add(panel_Center);
		panel_Center.setLayout(new GridLayout(0, 1, 0, 0));
		
		pDeptno = new InputPanel("부서번호");
		panel_Center.add(pDeptno);
		pDeptno.getTf().addFocusListener(this);
		
		pDeptName = new InputPanel("부서명");
		panel_Center.add(pDeptName);
		
		pFloor = new InputPanel("위치");
		panel_Center.add(pFloor);
		
		panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		if (!btnTxt.equals("추가")){
			panelDisable();
		}
		
		btnAdd = new JButton(btnTxt);
		if (btnTxt.equals("검색")){
			panelDisable();
		}
		btnAdd.addActionListener(this);
		panel_Bottom.add(btnAdd);
	}
	
	private Department getDept(){
		int dno = Integer.parseInt(pDeptno.getTf().getText().trim());
		String name = pDeptName.getTf().getText();
		int floor = Integer.parseInt(pFloor.getTf().getText());
		return new Department(dno, name, floor);
	}
	
	private void panelDisable() {
		pDeptno.getTf().setEnabled(true);
		pDeptName.getTf().setEnabled(false);
		pFloor.getTf().setEnabled(false);
	}
	
	private void panelEnable(){
		pDeptno.getTf().setEnabled(false);
		pDeptName.getTf().setEnabled(true);
		pFloor.getTf().setEnabled(true);
	}
	
	private void panelClear(){
		pDeptno.setTfTxt("");
		pDeptName.setTfTxt("");
		pFloor.setTfTxt("");
	}
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가"))
			actionPerformedBtnAdd(e);
		else if (e.getActionCommand().equals("수정")) 
			actionPerformedBtnUpdate(e);
		else if (e.getActionCommand().equals("검색"))
			actionPerformedBtnSearch(e);
		else if (e.getActionCommand().equals("삭제"))
			actionPerformedBtnDel(e);
	}
	
	protected void actionPerformedBtnAdd(ActionEvent e) {
		Department d = getDept();
		try {
			dao.insertItem(d);
			JOptionPane.showMessageDialog(this,"입력되었습니다");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"입력실패 다시 확인하세요.");
		}
	}
	
	private void actionPerformedBtnDel(ActionEvent e) {
		Department d = getDept();
		try {
			dao.deleteItemByNo(d);
			JOptionPane.showMessageDialog(this,"삭제되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
	}

	private void actionPerformedBtnUpdate(ActionEvent e) {
		Department d = getDept();
		
		try {
			dao.updateItemByNo(d);;
			JOptionPane.showMessageDialog(this,"수정되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"수정실패 다시 확인하세요.");
		}		
	}

	private void actionPerformedBtnSearch(ActionEvent e) {
		searchItem();
	}

	@Override
	public void focusGained(FocusEvent e) {}

	@Override
	public void focusLost(FocusEvent e) {
		searchItem();
		if (btnAdd.getText().equals("수정")){
			panelEnable();
		}
	}
	
	private void searchItem(){
		int no = Integer.parseInt(pDeptno.getTf().getText().trim());
		Department d = new Department();
		d.setDeptNo(no);
		try {
			d = dao.searchItem(d);
			pDeptName.setTfTxt(d.getDeptName());
			pFloor.setTfTxt(d.getFloor()+"");
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (NullPointerException e1){
			if (!btnAdd.getText().equals("추가"))
				JOptionPane.showMessageDialog(PanelDepartmentUI.this,"해당 부서가 없습니다.");
			else
				JOptionPane.showMessageDialog(PanelDepartmentUI.this,"추가 가능");
		}	
	}

}
